import { http } from "@/utils/http";

// 合同基本信息接口
export interface ContractItem {
  id: number;
  contractNumber: string;
  carNumber: string;
  tenantName: string;
  signDate: string;
  contractStatus: number;
  leaseTerm: string;
  endDate: string;
  startDate: string;
  depositFee: number;
  agencyFee: number;
  totalRent: number;
  remark: string;
  tenantPhone: string;
  parkingSpaceId: number;
  // 扩展字段，用于页面展示
  name: string; // 所属小区
  spaceNo: string; // 车位号
  contactPerson: string; // 联系人（对应tenantName）
  startPhone: string; // 联系电话（对应tenantPhone）
  chargeStandardCount: number; // 收费标准数量
  chargeStandards: string; // 收费金额
}

// 查询合同列表参数
export interface ContractListParams {
  page: number;
  limit: number;
  query?: string; // 搜索关键词
}

// 通用响应接口
export interface ApiResponse<T = any> {
  code: number;
  data?: T;
  message: string;
}

// 查询合同列表返回结果
export interface ContractListResult {
  code: number;
  data?: {
    list?: ContractItem[];
    total?: number;
  };
  message: string;
}

// 添加合同参数
export interface ContractAddPayload {
  contractNumber: string;
  carNumber: string;
  tenantName: string;
  signDate: string;
  contractStatus: number;
  leaseTerm: string;
  endDate: string;
  startDate: string;
  depositFee: number;
  agencyFee: number;
  totalRent: number;
  remark: string;
  tenantPhone: string;
  parkingSpaceId: number;
}

// 修改合同参数
export interface ContractUpdatePayload {
  id: number;
  contractNumber: string;
  carNumber: string;
  tenantName: string;
  signDate: string;
  contractStatus: number;
  leaseTerm: string;
  endDate: string;
  startDate: string;
  depositFee: number;
  agencyFee: number;
  totalRent: number;
  remark: string;
  parkingSpaceId: number;
}

// 续租参数
export interface ContractRenewalPayload {
  id: number;
  endDate: string;
}

// 只修改合同信息参数
export interface ContractUpdateOnePayload {
  id: number;
  contractNumber?: string;
  tenantName?: string;
  tenantContract?: string;
  contractStatus?: number;
  leaseTerm?: number;
  signDate?: string;
  endDate?: string;
  startDate?: string;
  deposit?: number;
  agencyFee?: number;
  totalRent?: number;
  remark?: string;
  tenantPhone?: string;
}

// 查询合同列表参数(oneList)
export interface ContractOneListParams {
  page: number;
  limit: number;
  query?: string;
}

// 查询租金信息参数
export interface FindChargesParams {
  spsId: number;
  tenantId: number;
}

// 小区车位信息接口
export interface ParkingSpaceInfo {
  id: number;
  // 根据实际返回数据添加其他字段
}

// 查询小区车位信息参数
export interface GetParkingSpaceParams {
  cellId: number;
}

/**
 * 查询合同列表
 * @param data 查询参数
 * @returns 合同列表数据
 */
export const getContractList = (data: ContractListParams) => {
  return http.request<ContractListResult>("post", "/contract/list", {
    data
  });
};

/**
 * 添加合同信息
 * @param data 合同信息
 * @returns 创建结果
 */
export const addContract = (data: ContractAddPayload) => {
  return http.request<ApiResponse>("post", "/contract/add", {
    data
  });
};

/**
 * 修改合同信息
 * @param data 合同信息（包含id）
 * @returns 修改结果
 */
export const updateContract = (data: ContractUpdatePayload) => {
  return http.request<ApiResponse>("post", "/contract/update", {
    data
  });
};

/**
 * 删除合同信息
 * @param id 合同ID
 * @returns 删除结果
 */
export const deleteContract = (id: number) => {
  return http.request<ApiResponse>("get", "/contract/delete", {
    params: { id }
  });
};

/**
 * 查询租金信息
 * @param data 查询参数
 * @returns 租金信息数据
 */
export const findCharges = (data: FindChargesParams) => {
  return http.request<ApiResponse>("post", "/contract/findCharges", {
    data
  });
};

/**
 * 根据id查询合同信息
 * @param id 合同ID
 * @returns 合同详情数据
 */
export const findContract = (id: number) => {
  return http.request<ApiResponse<ContractItem>>("get", "/contract/find", {
    params: { id }
  });
};

/**
 * 查询小区车位信息
 * @param cellId 小区ID
 * @returns 车位信息数据
 */
export const getParkingSpaceInfo = (cellId: number) => {
  return http.request<ApiResponse<ParkingSpaceInfo>>("get", "/contract/getPs", {
    params: { cellId }
  });
};

/**
 * 续租合同
 * @param data 续租信息（包含id和新的结束日期）
 * @returns 续租结果
 */
export const renewalContract = (data: ContractRenewalPayload) => {
  return http.request<ApiResponse>("post", "/contract/renewal", {
    data
  });
};

/**
 * 只修改合同信息
 * @param data 合同信息（包含id）
 * @returns 修改结果
 */
export const updateOneContract = (data: ContractUpdateOnePayload) => {
  return http.request<ApiResponse>("post", "/contract/updateOne", {
    data
  });
};

/**
 * 退租合同
 * @param id 合同ID
 * @returns 退租结果
 */
export const terminationContract = (id: number) => {
  return http.request<ApiResponse>("get", "/contract/termination", {
    params: { id }
  });
};

/**
 * 查询合同列表(oneList)
 * @param data 查询参数
 * @returns 合同列表数据
 */
export const getContractOneList = (data: ContractOneListParams) => {
  return http.request<ContractListResult>("post", "/contract/oneList", {
    data
  });
};